package com.sphd.emanu.activity.daydetail;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sphd.emanu.R;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.fragment.cardnum.CardNumFragment;
import com.sphd.emanu.fragment.daydetail.DayDetailFragment;
import com.sphd.emanu.fragment.time.DayDetailTimeFragment;
import com.sphd.emanu.fragment.time.TimeFragment;
import com.sphd.emanu.interfaces.DayDetailFragmentLetter;
import com.sphd.emanu.interfaces.Letter;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.pojo.MonthOrYearInfo;
import com.sphd.emanu.pojo.bank.AccountPeriodListByAccountBean;
import com.sphd.emanu.pojo.bank.TimeShowMoneyMessage;
import com.sphd.emanu.pojo.login.User;
import com.wzutils.L;
import com.wzutils.TimeUtils;

import java.text.ParseException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DayDetailActivity extends MVPBaseActivity<DayDetailContract.View, DayDetailPresenter> implements DayDetailContract.View, Letter, DayDetailFragmentLetter {

    int state;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.financial_balance_text)
    TextView financialBalanceText;
    @BindView(R.id.financial_balance_hint)
    TextView financialBalanceHint;
    @BindView(R.id.income_text)
    TextView incomeText;
    @BindView(R.id.income_hint)
    TextView incomeHint;
    @BindView(R.id.spending_text)
    TextView spendingText;
    @BindView(R.id.spend_hint)
    TextView spendHint;
    @BindView(R.id.last_money_text)
    TextView lastMoneyText;
    @BindView(R.id.last_money_hint)
    TextView lastMoneyHint;
    @BindView(R.id.buttom_balance_linear)
    LinearLayout buttomBalanceLinear;
    @BindView(R.id.layout_last_month_balance)
    LinearLayout layoutLastMonthBalance;
    Intent intent;
    Bundle bundle;
    String className = null;
    TimeShowMoneyMessage.AccountPeriodListByDateBean byDateBean;
    DayDetailFragment dayDetailFragment;
    TimeFragment timeFragment;
//    DayDetailTimeFragment timeFragment;
    User user;
    boolean isCardFrom = false;
    String beginDate, endDate;
    AccountPeriodListByAccountBean accountBean;
    int showType;
    int accountId=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day_detail);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener((v -> {
            finish();
        }));
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        className = bundle.getString(Constant.FLAG_NAME);
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        accountId=bundle.getInt(Constant.ID);
        if (accountId==0){
            toast("账户id=0");
        }else {
            toast("账户id="+accountId);
        }
        //判断是从哪个fragment过来的数据 分类接收数据
        if (className.equals(CardNumFragment.class.getName())){
            isCardFrom = true;
            accountBean = (AccountPeriodListByAccountBean) bundle.getSerializable(Constant.ACCOUNT_BEAN);
            state=accountBean.getState();
        }else {
            isCardFrom = false;
            byDateBean = (TimeShowMoneyMessage.AccountPeriodListByDateBean) bundle.getSerializable(Constant.LONG_NEAM_BEAN);
            state = byDateBean.getSatate();
            showType = byDateBean.getShowType();
        }
        beginDate=bundle.getString(Constant.Salary_User_Start);
        endDate=bundle.getString(Constant.Salary_User_End);
        toast(beginDate+"--------------------"+endDate);
        //判断要跳转到哪个fragment
        initFragment();
        getTopData();

    }



    private void getTopData() {
        if (isCardFrom) {
            setTopData();
        } else {
            mPresenter.getAllAccountData(setMapMessage("4", beginDate,
                    endDate, "2"));
        }
    }

    /**
     * 从账户查看跳转而来
     */
    private void setTopData() {
        setTitleBar(toolbar, accountBean.getAccountId().getBankName());
        financialBalanceText.setText(accountBean.getAccountId().getBalance() + "");
        incomeText.setText(accountBean.getAccountId().getCredit() + "");
        incomeHint.setText("本月收入");
        spendingText.setText(accountBean.getAccountId().getDebit() + "");
        spendHint.setText("本月支出");
        lastMoneyText.setText(accountBean.getAccountId().getPreviousBalance() + "");
        lastMoneyHint.setText("上月余额");
        financialBalanceHint.setText("当前余额" +
                TimeUtils.getDayAndTime(Calendar.getInstance(), accountBean.getBeginDate())[0] +
                "~" + TimeUtils.getDayAndTime(Calendar.getInstance(), accountBean.getEndDate())[0]);
    }


    /**
     *
     */
    private void initFragment() {

        dayDetailFragment = new DayDetailFragment();
        dayDetailFragment.setArguments(bundle);
        timeFragment = new TimeFragment();
//        timeFragment = new DayDetailTimeFragment();
        timeFragment.setArguments(bundle);
        //当showtype为3的时候跳转到时间轴明细展示的界面 如果是其他的就展示位报表 年月 日这里的showtype为上个界面的展示类型 切记
        if (showType==3) {
            setTitleBar(toolbar, "明细");
            buttomBalanceLinear.setVisibility(View.GONE);
            changeFragment(R.id.fragment_daylist_details, dayDetailFragment, Constant.Day);
            dayDetailFragment.setDetailFragmentLetter(this);
        } else {
            setTitleBar(toolbar, "报表");
            buttomBalanceLinear.setVisibility(View.VISIBLE);
            changeFragment(R.id.fragment_daylist_details, timeFragment, Constant.Month);
            timeFragment.setLetter(this);
        }
    }

    public Map setMapMessage(String state, String beginDate, String endDate, String monthOrDay) {
        Map<String, String> map = new HashMap<>();
        map.put("oid", user.getOid() + "");
        map.put("state", state);
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        map.put("monthOrDay", monthOrDay);
        return map;
    }

    @Override
    public void setBean(MonthOrYearInfo monthOrYearInfo) {
        financialBalanceText.setText(monthOrYearInfo.getAllBalance() + "");
        incomeText.setText(monthOrYearInfo.getAllCredit() + "");
        incomeHint.setText(showType==3 ? "今日收入" : "本月收入");
        spendingText.setText(monthOrYearInfo.getAllDebit() + "");
        spendHint.setText(showType==3 ? "今日支出" : "本月支出");

        lastMoneyText.setText(monthOrYearInfo.getAllPreviousBalance() + "");
        lastMoneyHint.setText("上月余额");
        financialBalanceHint.setText(showType==2 ? "当前余额" + monthOrYearInfo.getBeginDate() + "~" + monthOrYearInfo.getEndDate() :
                "今日余额");
    }

    /*利用回调得到到fragment是否加载完成*/
    @Override
    public void readyed() {
        //先进行判断是否是由cardfragment过来的
        timeFragment.swipeRefreshData(4,accountId,beginDate,endDate);
    }

    @Override
    public void readyedDayFragment() {
        dayDetailFragment.swfData(beginDate,accountId);
    }
}
